package com.springboot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer{
	@Autowired
	private JWTInterceptor jWTInterceptor;
	
	private static final Logger logger = LoggerFactory.getLogger(WebMvcConfig.class);
	
//	@Override
//	public void addInterceptors(InterceptorRegistry registry) {
//		logger.info("Adding JWTInterceptor");
//		registry.addInterceptor(jWTInterceptor)
//	            .addPathPatterns("/**")  //上面表示需要有token的验证/user/info就需要token
//	            .excludePathPatterns("/UserController/login" , "/UserController/register"); //对路径user下面的路径不需要token验证  不需要拦截，就是对controller里面的路径进行拦截或者不拦截
//	}
	
	@Bean
	public WebMvcConfigurer corsConfigurer() {
		logger.info("Configuring CORS");
		return new WebMvcConfigurer() {
			@Override
			public void addCorsMappings(CorsRegistry registry) {
				registry.addMapping("/**")
				.allowedOrigins("http://localhost:8081")
				//允许前端使用这个路径跨域访问
				.allowCredentials(true)
				.allowedMethods("GET", "POST", "DELETE", "PUT","PATCH")
				.allowedHeaders("*")
				.maxAge(36000);
			}
		};
	}
	
	
	
}
